<?php

function pre($data){
	echo "<pre>";print_r($data);echo "</pre>";
}

//URL安全的base64加密（+换-   /换_ 去掉“=”）
function enurl64($data) {
	return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}

//URL安全的base64解密
function deurl64($data) {
	return base64_decode(strtr($data, '-_', '+/'));
}


function curl_get($url, $header=[]){
	preg_match('/:([\d]{4})\//i', $url, $matches);
	$port = isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_PORT, $port);
	if(is_array($header) && count($header)>0){
		curl_setopt ($ch, CURLOPT_HTTPHEADER, $header );
	}
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

	$res = curl_exec($ch);
	$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch) ;
	//echo $res;
	return $res;
}

function curl_get_request($url, $params=[], $header=[]){
	preg_match('/:([\d]{4})\//i', $url, $matches);

	$port = isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;

	$uri = '';
	foreach($params as $key => $value){
		$uri .= '&'.$key.'='.$value;
	}

	$uri = preg_replace('/^&/', '?', $uri);

	$url = $url.$uri;

	//echo $url;exit;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_PORT, $port);
	if(is_array($header) && count($header)>0){
		curl_setopt ($ch, CURLOPT_HTTPHEADER, $header );
	}
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

	$res = curl_exec($ch);
	$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch) ;
	//echo $res;
	return $res;
}

function curl_post_header($url, $header, $formdata){
	preg_match('/:([\d]{4})\//i', $url, $matches);
	$port = isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;//根据URL自动获取端口

	$curl = curl_init();                                                // 启动一个CURL会话
	curl_setopt($curl, CURLOPT_URL, $url);                                // 要访问的地址
	curl_setopt($curl, CURLOPT_PORT, $port);							// 端口
	//HEADER
	if(is_array($header) && count($header)>0){
		curl_setopt ($curl, CURLOPT_HTTPHEADER, $header );
	}
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);                        // 对认证证书来源的检查
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);                        // 从证书中检查SSL加密算法是否存在
	curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
	// curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);                        // 使用自动跳转
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1);                            // 自动设置Referer
	curl_setopt($curl, CURLOPT_POST, 1);                                // 发送一个常规的Post请求
	curl_setopt($curl, CURLOPT_POSTFIELDS, $formdata);                        // Post提交的数据包
	// curl_setopt($curl, CURLOPT_COOKIEFILE, $GLOBALS['cookie_file']);    // 读取上面所储存的Cookie信息
	curl_setopt($curl, CURLOPT_TIMEOUT, 10);                            // 设置超时限制防止死循环
	curl_setopt($curl, CURLOPT_HEADER, 0);                                // 显示返回的Header区域内容
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);                        // 获取的信息以文件流的形式返回

	$temp_data = curl_exec($curl);                                     // 执行操作
	if (curl_errno($curl)) {
		// echo 'Errno'.curl_error($curl);                                // 返回curl错误信息
		return 'error';
	}
	curl_close($curl);                                                    // 关键CURL会话
	return $temp_data;                                                    // 返回临时数据
}

function curl_post_raw($url, $header, $raw) {

	preg_match('/:([\d]{4})\//i', $url, $matches);
	$port = isset($matches[1]) && !empty($matches[1]) ? $matches[1] : 80;//根据URL自动获取端口

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_PORT, $port);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	if(empty($header)){
		$header = [];
	}

	if(!empty($raw)){
		$raw_array = array_merge($header, [
			'X-AjaxPro-Method:ShowList',
			'Content-Type: application/json; charset=utf-8',
			'Content-Length: '.strlen($raw)
		]);
	}

	curl_setopt($ch, CURLOPT_HTTPHEADER, $raw_array);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

function indent($json) {

	$result = '';
	$pos = 0;
	$strLen = strlen($json);
	$indentStr = '&nbsp;&nbsp;&nbsp;&nbsp;';
	$newLine = "<br>";
	$prevChar = '';
	$outOfQuotes = true;

	for ($i=0; $i<=$strLen; $i++) {

		// Grab the next character in the string.
		$char = substr($json, $i, 1);
		// Are we inside a quoted string?
		if ($char == '"' && $prevChar != '\\') {
			$outOfQuotes = !$outOfQuotes;
			// If this character is the end of an element,
			// output a new line and indent the next line.
		} else if(($char == '}' || $char == ']') && $outOfQuotes) {
			$result .= $newLine;
			$pos--;
			for ($j=0; $j<$pos; $j++) {
				$result .= $indentStr;
			}
		}
		// Add the character to the result string.
		$result .= $char;
		// If the last character was the beginning of an element,
		// output a new line and indent the next line.
		if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
			$result .= $newLine;
			if ($char == '{' || $char == '[') {
				$pos++;
			}
			for ($j = 0; $j < $pos; $j++) {
				$result .= $indentStr;
			}
		}
		$prevChar = $char;
	}

	return $result;

}



